import asyncio
from multiprocessing import Process

from lagent.llms.spark_llm import SparkDeskLLM
from lagent.agents.interview_agent import InterviewAgent
from lagent.agents.aggregator.fewshot_aggregator import FewshotAggregator
from lagent.agents.question_agent import questionAgent
from lagent.agents.question_search import QuestionRetrievalEngine

# 初始化 LLM
llm = SparkDeskLLM(app_id='938ba839', api_key='5252debef08498da0080040f34d4e721', api_secret='ZWIwMWI1ODIyZTRiNDllZTFmMTFhZjZj', domain='x1') #x1
# llm = SparkDeskLLM(app_id='a8df86e8', api_key='4ceff67f3fce2460d945b751483f2bff', api_secret='OWIxMjA5NzBjODk5NzRmZjc4NDM2ZTdh', domain='lite')

# 设置候选人简历
resume_text = "姓名：刘柘远，意向岗位：前端开发工程师，技能：Java, SpringBoot, MySQL"
job = "前端工程师"
systemPrompt = ("你是一位经验丰富的计算机领域面试官，主要负责根据候选人的自我介绍和简历内容进行专业部分的提问，"
                "考虑到求职者是大学生群体没有丰富的项目经验，尽量减少提问技术优化之类的问题。以下是求职者的简历：") +resume_text



# 根据面试和自我介绍提问
async def chatByResume():
    # 初始化面试官 Agent
    agent = InterviewAgent(
        llm=llm,
        aggregator=FewshotAggregator(
            [
                {"role": "system", "content":systemPrompt}
            ]
        )
    )

    # agent.set_resume(resume_text)
    print(agent.preInterview())
    introduction = input('自我介绍：')
    question = agent.generete_FistQuestion(introduction)
    print("提问：",question)
    answer = input("我：")

    while True:
        question = agent.generate_normalQuestion(question,answer)
        print("提问：",question)
        if '面试拷打' in question:
            break
        answer = input("我：")
    print(agent.QandA())

# 提问八股专业问题
async def askTechQuestion():
    qs_Tool = QuestionRetrievalEngine("back_questions_with_vectors.json", "back_vectors.npy", "back_questions.index")
    qsAgent = questionAgent(llm, qs_Tool)
    user_profile = "5年Java开发经验，Redis" #简化版简历
    questionList = qsAgent.getProfessionQuestion(user_profile,"Java后端工程师","会使用Java多线程，对JVM有一定了解")
    print("AI面试官：", questionList)

async def run_all():
    await asyncio.gather(
        chatByResume(),
        # askTechQuestion()
    )
    # await chatByResume()
    # await askTechQuestion()
def run_chatByResume():
    asyncio.run(chatByResume())

def run_askTechQuestion():
    asyncio.run(askTechQuestion())

if __name__ == '__main__':
    asyncio.run(run_all())
    # process_chat = Process(target=run_chatByResume)
    # process_askTech = Process(target=run_askTechQuestion)
    # process_chat.start()
    # process_askTech.start()

    # process_chat.join()
    # process_askTech.join()